package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.record.monitor.MonitorIsolationExcelDto;
import com.lnking.lnkingplace.model.dto.record.monitor.MonitorListDto;
import com.lnking.lnkingplace.model.dto.record.monitor.MonitorWorkerExcelDto;
import com.lnking.lnkingplace.model.entity.IsolationPersonnel;
import com.lnking.lnkingplace.model.entity.IsolationWorker;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorAddIsolationRequest;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorAddRequest;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorSearchRequest;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationPersonnelService;
import com.lnking.lnkingplace.service.IIsolationWorkerService;
import com.lnking.lnkingplace.service.IRecordMonitorService;
import com.lnking.starter.export.utils.ExportUtils;
import com.lnking.starter.mybatis.entity.IPageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/record-monitor")
@Api(tags = "核酸检测管理")
public class RecordMonitorController {

    public static final String  TYPE="工作人员";

    @Resource
    IRecordMonitorService recordMonitorService;

    @Autowired
    IIsolationPersonnelService isolationPersonnelService;

    @Autowired
    IIsolationWorkerService iIsolationWorkerService;
    @Resource
    UserUtil userUtil;

    @ApiOperation("隔离人员查看健康记录")
    @GetMapping("get/info")
    public ReturnResult getInfo(@RequestParam String unionFlag){
        return ReturnResult.success(recordMonitorService.getInfo(unionFlag));
    }

    @ApiOperation("隔离人员新增")
    @PostMapping("add/isolation")
    public ReturnResult addIsolation(@RequestBody List<MonitorAddIsolationRequest> params){
        recordMonitorService.addIsolation(params);
        return ReturnResult.success();
    }

    @ApiOperation(value = "隔离人员导出")
    @GetMapping("export-personnel")
    public void exportPersonnel(MonitorSearchRequest params, HttpServletResponse response) throws Exception {
        params.setPersonnelType("隔离人员");
        List<MonitorListDto> data = (List<MonitorListDto>)recordMonitorService.pageList(params);
        ExportUtils.export(getGroupData(data), response, MonitorIsolationExcelDto.class);
    }

    @ApiOperation(value = "工作人员导出")
    @GetMapping("export-worker")
    public ReturnResult exportWorker(MonitorSearchRequest params, HttpServletResponse response) throws Exception {
        params.setPersonnelType("工作人员");
        List<MonitorListDto> data = (List<MonitorListDto>)recordMonitorService.pageList(params);
        ExportUtils.export(getGroupData(data), response, MonitorWorkerExcelDto.class);
        return ReturnResult.success();
    }

    @ApiOperation("隔离人员导入")
    @PostMapping("importIsolation")
    private ReturnResult importIsolation(@RequestParam("file") MultipartFile file) throws Exception{
        recordMonitorService.importIsolation(file);
        return ReturnResult.success();
    }

    @ApiOperation("工作人员导入")
    @PostMapping("importWorker")
    private ReturnResult importWorker(@RequestParam("file") MultipartFile file) throws Exception{
        recordMonitorService.importWorker(file);
        return ReturnResult.success();
    }

    @ApiOperation("查询")
    @GetMapping("list")
    public ReturnResult getList(MonitorSearchRequest params){
        String type = userUtil.getUser().getType();
        //隔离场所
        if(type.equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }

        if(params.getId()!=null){
            if (params.getPersonnelType().equals(TYPE)) {
                IsolationWorker byId = iIsolationWorkerService.getById(params.getId());
                params.setPhone(byId.getWorkerPhone());
            }else{
                IsolationPersonnel byId = isolationPersonnelService.getById(params.getId());
                params.setPhone(byId.getPhone());
            }
        }
        IPageInfo data = (IPageInfo)recordMonitorService.pageList(params);
        List<MonitorListDto> list = (List<MonitorListDto>)data.getList();
        data.setList(getGroupData(list));
        return ReturnResult.success(data);
    }

    private List<MonitorListDto> getGroupData(List<MonitorListDto> data ){
        List<MonitorListDto> result = data.stream().sorted(Comparator.comparing(MonitorListDto::getCreateTime).reversed())
                .collect(Collectors.groupingBy(MonitorListDto::getPlaceName, Collectors.groupingBy(MonitorListDto::getName)))
                .entrySet().stream()
                .flatMap(p -> p.getValue().entrySet().stream().flatMap(a -> a.getValue().stream()))
                .collect(Collectors.toList());
        return result;
    }

    @ApiOperation("新增")
    @PostMapping("add")
    public ReturnResult add(@Valid @RequestBody MonitorAddRequest params){
        recordMonitorService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public ReturnResult update(@Valid @RequestBody MonitorUpdateRequest params){
        recordMonitorService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(recordMonitorService.getById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        recordMonitorService.removeById(id);
        return ReturnResult.success();
    }
}
